package com.jiaoxue.tasknotes.addTask;

import android.arch.persistence.room.Room;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.jiaoxue.tasknotes.Bean.TaskBean;
import com.jiaoxue.tasknotes.MainActivity;
import com.jiaoxue.tasknotes.R;
import com.jiaoxue.tasknotes.adapter.TaskAdapter;
import com.jiaoxue.tasknotes.db.AppDatabase;
import com.jiaoxue.tasknotes.db.TaskDao;

import java.util.List;

public class AddTaskActivity extends AppCompatActivity {
    private EditText edittitle;
    private EditText editcontent;
    private Button btnok;
    TaskBean taskBean = new TaskBean();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_task);
        edittitle = findViewById(R.id.edittitle);
        editcontent = findViewById(R.id.editcontent);
        btnok = findViewById(R.id.btnok);
        final TaskDao taskDao = Room.databaseBuilder(getApplicationContext(), AppDatabase.class, "tasks").build().taskDao();

        btnok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                taskBean.setTitle(edittitle.getText().toString().trim());
                taskBean.setContent(editcontent.getText().toString().trim());
                new DataBaseAsyncTask().execute();
            }


        });
    }


    class DataBaseAsyncTask extends AsyncTask<Void, Void, Boolean> {
        TaskDao taskDao;

        @Override
        protected Boolean doInBackground(Void... voids) {
            taskDao.intserAll(taskBean);
            return true;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            taskDao = Room.databaseBuilder(getApplicationContext(), AppDatabase.class, "tasks").build().taskDao();
        }

        @Override
        protected void onPostExecute(Boolean aBoolean) {
            super.onPostExecute(aBoolean);
            finish();
        }
    }

}
